#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
SFT训练脚本
"""

import importlib.util
import os
import sys

# 添加项目根目录到Python路径
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, project_root)

from plugins.trainers.sft_trainer import SFTTrainer


def load_config(config_path):
    """加载配置文件"""
    spec = importlib.util.spec_from_file_location("config", config_path)
    config_module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(config_module)
    return config_module.config


def main():
    # 检查命令行参数
    if len(sys.argv) < 2:
        print("Usage: python train_sft.py <config_file>")
        sys.exit(1)
    
    config_file = sys.argv[1]
    
    # 加载配置文件
    if not os.path.exists(config_file):
        print(f"Error: Config file '{config_file}' not found!")
        sys.exit(1)
        
    config = load_config(config_file)
    
    # 创建并运行训练器
    trainer = SFTTrainer(config)
    trainer.train()


if __name__ == "__main__":
    main()